# 显示图片，构建一个18*n大小(n=batch_size/16)的图片阵列，把预测的图片打印到note中。
import matplotlib.pyplot as plt
import numpy as np
# % matplotlib inline

"""
%matplotlib具体作用是当你调用matplotlib.pyplot的绘图函数plot()进行绘图的时候，或者生成一个figure画布的时候，可以直接在你的python console里面生成图像
"""


def show_image_grid(images, batch_size=128, pass_id=None):
    fig = plt.figure(figsize=(8, batch_size / 32))
    fig.suptitle("Pass {}".format(pass_id))
    gs = plt.GridSpec(int(batch_size / 16), 16)
    gs.update(wspace=0.05, hspace=0.05)

    for i, image in enumerate(images):
        ax = plt.subplot(gs[i])
        plt.axis('off')
        ax.set_xticklabels([])
        ax.set_yticklabels([])
        ax.set_aspect('equal')
        plt.imshow(image[0], cmap='Greys_r')

    plt.show()


# 拼接一个batch图像用于VisualDL可视化
def concatenate_img(input_img, batch_size = 128):
    img_arr_broadcasted = ((np.zeros([batch_size, 3, 28, 28]) + input_img) * 255).astype('uint8').transpose(
        (0, 2, 3, 1)).reshape([-1, 16, 28, 28, 3])
    # print(img_arr_broadcasted.shape)
    img_concatenated = np.concatenate(tuple(img_arr_broadcasted), axis=1)
    # print(img_concatenated.shape)
    img_concatenated = np.concatenate(tuple(img_concatenated), axis=1)
    # print(img_concatenated.shape)
    return img_concatenated